package com.wuseguang.springcloud.controller;

import com.wuseguang.springcloud.Filter.RequestParameterWrapper;
import com.wuseguang.springcloud.service.DisciplineEstablishmentService;
import com.wuseguang.springcloud.util.CodeUtil;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @描述 数据管理-->学科设置
 * @创建人 zm
 * @创建时间 2019/12/11
 */
@RestController
@RequestMapping("/subject")
public class DisciplineEstablishmentController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private DisciplineEstablishmentService service;

    /**
     * @描述 查询所有学科
     * @创建人 zm
     * @创建时间 2019/12/11
     */
    @RequestMapping("/querySubjects")
    public Result querySubjects(RequestParameterWrapper request) {
        String schoolId = request.getParam("schoolId");
        logger.info("schoolId={}", schoolId);
        return new Result(service.querySubjects(schoolId));
    }

    /**
     * @描述 添加科目
     * @创建人 zm
     * @创建时间 2019/12/11
     */
    @RequestMapping("/addSubjects")
    public Result addSubjects(RequestParameterWrapper request, @RequestParam String name) {
        String schoolId = request.getParam("schoolId");
        logger.info("schoolId={}", schoolId);
        int i1 = service.querySubjectsCountandName(schoolId, name);
        if (i1 > 0) {
            return new Result(CodeUtil.ACCOUNT_NAME_ALREADY_EXISTS);
        }
        Long i = service.addSubjects(schoolId, name);
        if (i > 0) {
            HashMap<String, Long> map = new HashMap<>();
            map.put("id", i);
            return new Result(map);
        }
        return new Result(CodeUtil.ADD_FAILURE);
    }

    /**
     * @描述 修改科目名称
     * @创建人 zm
     * @创建时间 2019/12/11
     */
    @RequestMapping("/updateSubjects")
    public Result updateSubjects(RequestParameterWrapper request, @RequestParam String id, @RequestParam String name) {
        String schoolId = request.getParam("schoolId");
        logger.info("schoolId={}", schoolId);
        int i1 = service.querySubjectsCountandName(schoolId, name);
        if (i1 > 0) {
            return new Result(CodeUtil.ACCOUNT_NAME_ALREADY_EXISTS);
        }
        int i = service.updateSubjects(id, name);
        if (i > 0) {
            return new Result();
        }
        return new Result(CodeUtil.MODIFICATION_FAILED);
    }

    /**
     * @描述 删除科目
     * @创建人 zm
     * @创建时间 2019/12/11
     */
    @RequestMapping("/delSubjects")
    public Result delSubjects(@RequestParam String id) {
        int i = service.delSubjects(id);
        if (i > 0) {
            return new Result();
        }
        return new Result(CodeUtil.DELETE_FAILED);
    }

    /**
     * @描述 验证科目是否被使用
     * @创建人 zm
     * @创建时间 2020/2/10
     */
    @RequestMapping("/querySubjectsById")
    public Result querySubjectsById(@RequestParam("id") String id) {
        int i = service.querySubjectsById(id);
        if (i > 0) {
            return new Result(CodeUtil.TEACHING_SUBJECT);
        }
        return new Result();
    }
}
